package cl.uchile.cos.sync;

import java.io.Serializable;

import cl.uchile.cos.sync.adapters.ServerAdapter;
import cl.uchile.cos.sync.listeners.MessageListener;

public class MessagePropagator implements QueuedTask, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -904517305817437747L;
	private Message message;
	private ServerAdapter adapter;
	private boolean echo;
	private CouplingManagerImpl manager;

	public MessagePropagator(Message message, ServerAdapter adapter,
			boolean echo, CouplingManagerImpl manager) {
		this.message = message;
		this.adapter = adapter;
		this.echo = echo;
		this.manager = manager;
	}

	public void execute() {
		synchronized (manager) {
 
			for (MessageListener listener : manager.getMessageListeners()) {
				listener.onRecive(message, adapter);
			}
			manager.propagate(message, adapter, echo);
		}
	}
}
